package me.yricky.mayu.ui.fasta

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.AssignmentReturn
import androidx.compose.material.icons.rounded.Done
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import me.yricky.mayu.utils.asList
import me.yricky.mayu.utils.string
import java.awt.Dimension

/**
 * @author Yricky
 * @date 2022/4/24
 */

@Composable
fun NewSequenceDialog(onClose:()->Unit, seq: FastaSeqModel = FastaSeqModel(), onCreate:(FastaSeqModel)->Unit = {}){
    Dialog(onClose, title = "新建序列", icon = rememberVectorPainter(Icons.Rounded.Add)){
        LaunchedEffect(Unit){
            window.minimumSize = Dimension(800,600)
        }
        var fastaSeq by remember { mutableStateOf(seq) }
        Scaffold(
            floatingActionButton = {
                FloatingActionButton({
                    onCreate(fastaSeq)
                    onClose()
                }){ Icon(Icons.Rounded.Done,"") }
            }
        ) {
            Column(Modifier.padding(8.dp)) {
                OutlinedTextField(
                    fastaSeq.name,
                    { fastaSeq.name = it },
                    Modifier.fillMaxWidth().padding(8.dp,4.dp),
                    label = { Text("序列名") },
                    singleLine = true
                )
                val onSeqContent:(CharSequence)->Unit = remember {
                    {
                        fastaSeq.seq.apply {
                            clear()
                            addAll(
                                it.replace(Regex("[^A-Za-z*\\-]"),"").uppercase().asList()
                            )
                        }
                    }
                }
                OutlinedTextField(
                    fastaSeq.seq.string(),
                    onSeqContent,
                    Modifier.fillMaxWidth().padding(8.dp,4.dp),
                    trailingIcon = {
                        val cbm = LocalClipboardManager.current
                        Icon(
                            Icons.Rounded.AssignmentReturn,"",
                            modifier = Modifier.clickable(onClickLabel = "从剪贴板读取") {
                            cbm.getText()?.let(onSeqContent)
                        }.padding(8.dp))
                    },
                    singleLine = true
                )
                fastaSeq.DetailContent(Modifier.fillMaxWidth().weight(1f)){
                    fastaSeq.PreviewSequenceGrid{ Spacer(Modifier.height(120.dp)) }
                }
            }
        }

    }
}